import {
    GET_ACCOUNT_DETAIL,
    GET_BANKINFO_DETAIL, SET_BANKINFO_DETAIL,
    APPLY_CRASH, GET_RECORD_LIST, GET_TRANSATION_LIST, CHOOSE_BANK
} from './constants';
import NetUtil from '../../common/NetUtil';
import config from '../../config/config';
import {toast} from '../../utils/NativeUtil';

/**
 * 重置reduceer
 * @returns {function(*)}
 */
export const resetState = () => {
    return dispatch => {
        dispatch({
            type: 'reset'
        })
    }
};


//获取账户详情
export const getDetailAction = (token) => {

    return dispatch => {
        const params = {
            token: token
        };
        let url = "/s/account/detail";
        NetUtil.postSign(config.apiUrl + url, params, function (res) {
            if (res.success) {
                let data = res.data;
                dispatch({
                    type: GET_ACCOUNT_DETAIL,
                    payload: {
                        accountId: data.accountId,
                        activeAmount: data.activeAmount,
                        frozenAmount: data.frozenAmount,
                        totalAmount: data.totalAmount,
                        amount: data.amount,

                    }
                });
            } else {
                toast(res.message);
            }
        });
    }
};

//获取银行卡信息
export const getBankAccountDetailAction = (token) => {
    return dispatch => {
        const params = {
            token: token
        };
        let url = "/s/moneyRecordAccount/detail";
        NetUtil.postSign(config.apiUrl + url, params, function (res) {
            console.log(res);
            let data = res.data;
            if (data != undefined) {
                dispatch({
                    type: GET_BANKINFO_DETAIL,
                    payload: {
                        accountName: data.accountName,
                        bankId: data.bankId,
                        bankName: data.bankName,
                        recordAccountId: data.recordAccountId,
                        bankDetail: data.bankDetail,
                        bankArea: data.city
                    }
                });
            }
        });
    }
};

//获取编辑银行卡验证码
export const getCodeAction = (token, success) => {
    return dispatch => {
        const params = {
            token: token
        };
        let url = '/s/moneyRecordAccount/sms';
        NetUtil.postSign(config.apiUrl + url, params, function (res) {
            if (res.success) {
                success();
            } else {
                toast(res.message);
            }
        })
    }
};

//保存银行卡信息
export const saveBankInfoAction = (flag, params, success) => {
    return dispatch => {
        let url = '';
        if (flag == 'add') {
            url = '/s/moneyRecordAccount/add2';
        } else {
            url = "/s/moneyRecordAccount/update2";
        }
        NetUtil.postSign(config.apiUrl + url, params, function (res) {
            if (res.success) {
                dispatch({
                    type: SET_BANKINFO_DETAIL,
                    payload: {
                        accountName: params.accountName,
                        bankId: params.bankId,
                        bankName: params.bankName,
                        bankDetail: params.bankDetail
                    }
                });
                success();
            } else {
                toast(res.message);
            }
        });
    }
};

//提现申请
export const applyCrashAction = (params, success) => {
    return dispatch => {
        let url = '/s/moneyRecord/apply';
        NetUtil.postSign(config.apiUrl + url, params, function (res) {
            if (res.success) {
                success();
                dispatch({
                    type: APPLY_CRASH,
                    payload: params
                });
            } else {
                toast(res.message);
            }
        })
    }
};

//获取提现记录
export const getRecordAction = (params, success) => {
    return dispatch => {
        let url = '/s/moneyRecord/recordList';
        NetUtil.postSign(config.apiUrl + url, params, function (res) {
            if (res.success) {

                let hasmore = false;
                if (res.data.length < 20) {
                    hasmore = false;
                } else {
                    hasmore = true;
                }

                success({
                    hasMore: hasmore
                });
                dispatch({
                    type: GET_RECORD_LIST,
                    payload: {
                        recordList: res.data,
                        params: params
                    }
                });
            } else {

            }
        })
    }
};

//交易明细
export const getTransactionAction = (params, success) => {
    return dispatch => {
        let url = "/s/transaction/getShopTransactions";//交易明细

        NetUtil.postSign(config.apiUrl + url, params, function (res) {
            if (res.success) {
                let hasmore = false;
                if (res.data.list.length < 20) {
                    hasmore = false;
                } else {
                    hasmore = true;
                }
                success({
                    hasMore: hasmore
                });

                dispatch({
                    type: GET_TRANSATION_LIST,
                    payload: {
                        transationList: res.data.list,
                        params: params
                    }
                });
            }
        })
    }
};

//银行卡要素检测
export const BankInfoCheckAction = (acct_name, acct_pan, cert_id, phone_num, callback) => {
    let url = "http://ali-bankcard4.showapi.com/bank3";
    let params = {
        acct_name: acct_name,
        acct_pan: acct_pan,
        cert_id: cert_id,
        cert_type: '01',
        needBelongArea: 'true',
    };

    NetUtil.getAliyun(url, '1eb4a6c56e114f80ab0dc13310aee0f1', params, (data) => {
        callback(data);
    });
};


export const getBankListAction = (params, callback) => {
    let url = "/s/bank/search_branch";
    NetUtil.postSign(config.apiUrl + url, params, (res) => {
        if (res.success) {
            callback(res);
        }
    })
};


export const chooseBank = (bankname) => {
    return dispatch => {
        dispatch({
            type: CHOOSE_BANK,
            payload: {
                bankDetail: bankname
            }
        });
    }
};